<template>
  <div>
    <el-card class="box-card">
      <span>商品分类：</span>
      <el-tag v-for="item in objs" :key="item.id">{{item.name}}</el-tag>
      <i class="el-icon-edit" @click="topStep">编辑</i>
    </el-card>
    <el-card class="box-card">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称：" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="副标题：" prop="caption">
          <el-input v-model="ruleForm.caption"></el-input>
        </el-form-item>
        <el-form-item label="商品品牌：" prop="brandId">
          <el-select v-model="ruleForm.brandId" placeholder="请选择品牌" @change="setBrandName">
            <el-option
              v-for="item in initList.brands"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品介绍：" prop="introduction">
          <el-input type="textarea" v-model="ruleForm.desc"></el-input>
        </el-form-item>
        <el-form-item label="运费模板: " prop="freightId">
          <el-select v-model="ruleForm.freightId" placeholder="请选择活动区域">
            <el-option label="没查" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品货号：" prop="sn">
          <el-input v-model="ruleForm.sn"></el-input>
        </el-form-item>
        <span>如果您不输入商品货号，系统将自动生成一个唯一的货号。</span>
        <el-form-item label="服务保证：" prop="saleService">
          <el-checkbox-group v-model="checkdName">
            <el-checkbox
              v-for="item in checkList"
              :key="item.id"
              :label="item.name"
              v-model="item.checked"
            >{{item.name}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="topStep()">上一步，选择商品分类</el-button>
          <el-button @click="onSubmit()">下一步，填写商品属性</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      checkList: [{ name: "无忧退货", id: "1", checked: true },
      { name: "快速退款", id: "2" },
      { name: "免费包邮", id: "3" }],
      checkdName: [],
      ruleForm: {
        saleService: '',
        name: '蜡笔小新',
        caption: '番外篇',
        desc: '限售十本',
        sn: '45646'
      },
      checked: true,
      rules: {
        name: [
          { required: true, message: '商品名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        region: [
          { required: true, message: '副标题', trigger: 'change' }
        ],
        resource: [
          { required: true, message: '请选择活动资源', trigger: 'change' }
        ],
        desc: [
          { required: true, message: '请填写活动形式', trigger: 'blur' }
        ]
      },
      data: this.objs,
      initList: []
    }
  },
  methods: {
    // 上一步
    topStep () {
      this.objs.push({ active: 1 })
      this.$emit('listenToChildEvent', this.objs)
    },
    // 下一步
    onSubmit () {
      this.checkdName.forEach(element => {
        this.ruleForm.saleService += element + ","
      })
      const index = this.ruleForm.saleService.length - 1
      this.ruleForm.saleService = this.ruleForm.saleService.substring(0, index)
      this.nextStep()
    },
    // 还没写  一定到下一步 没有做验证
    nextStep (ruleForm) {
      this.ruleForm.active = 3
      this.$emit('nextStep3', this.ruleForm)
    },
    setBrandName () {
      this.initList.brands.forEach(element => {
        if (this.ruleForm.brandId === element.id) {
          this.ruleForm.brandName = element.name
        }
      })
    }
  },
  props: {
    objs: Array
  },
  async created () {
    let index = 0
    if (this.objs !== undefined && this.objs.length > 0) {
      index = this.objs.length - 1
      console.log(this.objs[index])
      if (index === 3) {
        this.ruleForm = this.objs[index]
      } else {
        this.ruleForm.category1Id = this.objs[0].id
        this.ruleForm.category2Id = this.objs[1].id
        this.ruleForm.category3Id = this.objs[2].id
        this.ruleForm.templateId = this.objs[2].templateId
        this.ruleForm.categoryMame = this.objs[2].name // 三级分类name
      }
    }
    const { data: res } = await this.$http.get(`goods/spu/initBrand?categoryId=${this.ruleForm.category3Id}`)
    console.log(res)
    this.initList = res
  }
}
</script>

<style  scoped>
</style>
